home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVTIFF.C < prev    next >
C/C++ Source or Header  |  1993-05-19  |  28KB  |  768 lines

  1. /* $Header: /usr/people/sam/fax/gs/RCS/gdevtiff.c,v 1.3 93/03/20 11:44:02 sam Exp $ */
  2.  
  3. /*
  4.  * Copyright (c) 1992, 1993 Sam Leffler
  5.  * Copyright (c) 1992, 1993 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26. /*
  27.  * 5/19/93 modified by L. Peter Deutsch, Aladdin Enterprises,
  28.  *   for compatibility with Ghostscript 2.6.1.
  29.  */
  30. /* gdevtiff.c */
  31.  
  32. #include "gdevprn.h"
  33. #include "gdevdfg3.h"
  34. #include "gdevtiff.h"
  35.  
  36. #ifdef __PROTOTYPES__
  37. #    define PROTO_ARGS(X)    X
  38. #else
  39. #    define PROTO_ARGS(X)    ()
  40. #endif
  41.  
  42. /*
  43.  * TIFF fax output driver.
  44.  */
  45. typedef struct {
  46.     FILE*    fp;
  47.     long    prevdir;    /* file offset of previous directory offset */
  48.     long    diroff;        /* file offset of next write */
  49.     int        bigendian;    /* 1 if machine is big-endian, 0 otherwise */
  50.     int        fax_byte;
  51.     int        fax_weight;
  52. } TIFFOUT;
  53.  
  54. private void    faxout_open_fp PROTO_ARGS((FILE *, TIFFOUT*));
  55. private int    faxout_begin_page PROTO_ARGS((TIFFOUT*, gx_device_printer*));
  56. private int    faxout_eolcode PROTO_ARGS((TIFFOUT *));
  57. private int    faxout_end_page PROTO_ARGS((TIFFOUT *));
  58. private void    tofax PROTO_ARGS((TIFFOUT*, unsigned char*));
  59.  
  60. private void putwhitespan();
  61. private void putblackspan();
  62. private void putcode();
  63. private void puteol();
  64. private void putbit();
  65. private void flushbits();
  66.  
  67. /*
  68.  * Redefine the device descriptor.
  69.  */
  70. struct gx_device_tiff_s {
  71.     gx_device_common;
  72.     gx_prn_device_common;
  73.     TIFFOUT    fax;
  74. };
  75. typedef struct gx_device_tiff_s gx_device_tiff;
  76.  
  77. /* The device descriptor */
  78. #define X_DPI 204
  79. #define Y_DPI 196
  80. #define LINE_SIZE ((X_DPI * 98 / 10 + 7) / 8)    /* bytes per line */
  81.  
  82. private dev_proc_open_device(tiff_prn_open);
  83. private dev_proc_print_page(tiff_print_page);
  84. private dev_proc_close_device(tiff_prn_close);
  85.  
  86. gx_device_procs tiff_std_procs =
  87.     prn_procs(tiff_prn_open, gdev_prn_output_page, tiff_prn_close);
  88.  
  89. gx_device_tiff far_data gs_tiffg3_device =
  90. {   prn_device_std_body(
  91.     gx_device_tiff,
  92.     tiff_std_procs,
  93.     "tiffg3",
  94.     85,            /* width_10ths, 8.5" */
  95.     110,            /* height_10ths, 11" */
  96.     X_DPI, Y_DPI,
  97.     0,0,0,0,        /* margins */
  98.     1,
  99.     tiff_print_page
  100.     )
  101. };
  102.  
  103.  
  104. static struct pageinfo {
  105.     short w, h;            /* page width and height in 10ths */
  106.     unsigned long iw;        /* image width */
  107. } pageinfo[] = {
  108. #define    PAPER_SIZE_LETTER    0
  109.     { 85,  110, 1728 },
  110. #define    PAPER_SIZE_A4        1
  111.     { 83,  117, 1728 },
  112. #define    PAPER_SIZE_B4        2
  113.     { 98, 1391, 2048 }
  114. };
  115. #define    NPAGEINFO (sizeof (pageinfo) / sizeof (pageinfo[0]))
  116.  
  117. /* Get the paper size code, based on width and height. */
  118. static int
  119. papersize(gx_device *dev)
  120. {
  121.     return
  122.       (dev->height / dev->y_pixels_per_inch >= 11.8 ? PAPER_SIZE_B4 :
  123.        dev->height / dev->y_pixels_per_inch >= 11.1 ? PAPER_SIZE_A4 :
  124.        PAPER_SIZE_LETTER);
  125. }
  126.  
  127. /*
  128.  * Driver entry points.
  129.  */
  130.  
  131. /*
  132.  * Setup device according to output page.
  133.  */
  134. private int
  135. tiff_prn_open(gx_device *pdev)
  136. {
  137.     struct pageinfo* pi = &pageinfo[papersize(pdev)];
  138.     int    rc;
  139.  
  140.     pdev->width = (int)((pi->w * pdev->x_pixels_per_inch) / 10);
  141.     pdev->height = (int)((pi->h * pdev->y_pixels_per_inch) / 10);
  142.     rc = gdev_prn_open(pdev);
  143.     if (rc == 0) {
  144.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  145.     faxout_open_fp(ddev->file, &ddev->fax);
  146.     }
  147.     return (rc);
  148. }
  149.  
  150. private int
  151. tiff_print_page(gx_device_printer *pdev, FILE *prn_stream)
  152. {
  153.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  154.     unsigned char data[LINE_SIZE + 4];
  155.     int    lnum, line_size;
  156.     TIFFOUT* fax = &ddev->fax;
  157.  
  158.     /* For some odd reason, the file isn't open until now */
  159.     fax->fp = prn_stream;    
  160.     faxout_begin_page(fax, pdev);
  161.     line_size = gdev_mem_bytes_per_scan_line((gx_device*)pdev);
  162.     for (lnum = 0; lnum < pdev->height; lnum++) {
  163.     gdev_prn_copy_scan_lines(pdev, lnum, (byte *)data, line_size);
  164.     tofax(fax, data);
  165.     }
  166.     faxout_end_page(fax);
  167.     return (0);
  168. }
  169.  
  170. private int
  171. tiff_prn_close(gx_device *pdev)
  172. {
  173.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  174.     TIFFOUT* fax = &ddev->fax;
  175.  
  176.     if (fax->fp)
  177.     fflush(fax->fp);
  178.     return (gdev_prn_close(pdev));
  179. }
  180.  
  181. /*
  182.  * Internal routines.
  183.  */
  184. private void
  185. faxout_open_fp(FILE *fp, register TIFFOUT* faxp)
  186. {
  187.     faxp->fp = fp;
  188.     faxp->diroff = 0L;
  189.     faxp->prevdir = 0L;
  190.     faxp->bigendian = arch_is_big_endian;
  191.     faxp->fax_byte = 0;
  192.     faxp->fax_weight = 0x80;
  193. }
  194.  
  195. /* NB: this array is sorted by tag number (assumed below) */
  196. typedef struct {
  197.     TIFFDirEntry    subfiletype;
  198.     TIFFDirEntry    imagewidth;
  199.     TIFFDirEntry    imagelength;
  200.     TIFFDirEntry    bitspersample;
  201.     TIFFDirEntry    compression;
  202.     TIFFDirEntry    photometric;
  203.     TIFFDirEntry    fillorder;
  204. #ifdef notdef
  205.     TIFFDirEntry    documentname;
  206. #endif
  207.     TIFFDirEntry    stripoffsets;
  208.     TIFFDirEntry    orientation;
  209.     TIFFDirEntry    samplesperpixel;
  210.     TIFFDirEntry    rowsperstrip;
  211.     TIFFDirEntry    stripbytecounts;
  212.     TIFFDirEntry    xresolution;
  213.     TIFFDirEntry    yresolution;
  214.     TIFFDirEntry    planarconfig;
  215.     TIFFDirEntry    group3options;
  216.     TIFFDirEntry    resolutionunit;
  217. #ifdef notdef
  218.     TIFFDirEntry    software;
  219. #endif
  220.     TIFFDirEntry    cleanfaxdata;
  221.     unsigned long    diroff;            /* offset to next directory */
  222.     unsigned long    xresValue[2];        /* xresolution indirect value */
  223.     unsigned long    yresValue[2];        /* yresolution indirect value */
  224. } TIFFDirectory;
  225. private TIFFDirectory dirTemplate = {
  226.     { TIFFTAG_SUBFILETYPE,    TIFF_LONG,  1, FILETYPE_PAGE },
  227.     { TIFFTAG_IMAGEWIDTH,    TIFF_LONG,  1 },
  228.     { TIFFTAG_IMAGELENGTH,    TIFF_LONG,  1 },
  229.     { TIFFTAG_BITSPERSAMPLE,    TIFF_SHORT, 1, 1 },
  230.     { TIFFTAG_COMPRESSION,    TIFF_SHORT, 1, COMPRESSION_CCITTFAX3 },
  231.     { TIFFTAG_PHOTOMETRIC,    TIFF_SHORT, 1, PHOTOMETRIC_MINISWHITE },
  232.     { TIFFTAG_FILLORDER,    TIFF_SHORT, 1, FILLORDER_MSB2LSB },
  233. #ifdef notdef
  234.     { TIFFTAG_DOCUMENTNAME,    TIFF_ASCII, 1 },
  235. #endif
  236.     { TIFFTAG_STRIPOFFSETS,    TIFF_LONG,  1 },
  237.     { TIFFTAG_ORIENTATION,    TIFF_SHORT, 1, ORIENTATION_TOPLEFT },
  238.     { TIFFTAG_SAMPLESPERPIXEL,    TIFF_SHORT, 1, 1 },
  239.     { TIFFTAG_ROWSPERSTRIP,    TIFF_LONG,  1, -1L },
  240.     { TIFFTAG_STRIPBYTECOUNTS,    TIFF_LONG,  1, 1 },
  241.     { TIFFTAG_XRESOLUTION,    TIFF_RATIONAL, 1 },
  242.     { TIFFTAG_YRESOLUTION,    TIFF_RATIONAL, 1 },
  243.     { TIFFTAG_PLANARCONFIG,    TIFF_SHORT, 1, PLANARCONFIG_CONTIG },
  244.     { TIFFTAG_GROUP3OPTIONS,    TIFF_LONG,  1 },
  245.     { TIFFTAG_RESOLUTIONUNIT,    TIFF_SHORT, 1, RESUNIT_INCH },
  246. #ifdef notdef
  247.     { TIFFTAG_SOFTWARE,        TIFF_ASCII, 1 },
  248. #endif
  249.     { TIFFTAG_CLEANFAXDATA,    TIFF_SHORT, 1, CLEANFAXDATA_CLEAN },
  250.     0, { 0, 1 }, { 0, 1 },
  251. };
  252. #define    OFFSET(x)    ((unsigned)&(((TIFFDirectory*)0)->x))
  253. #define    NTAGS        (OFFSET(diroff) / sizeof (TIFFDirEntry))
  254.  
  255. /* correct tag values on bigendian machines */
  256. private void
  257. faxout_fixuptags(TIFFDirEntry* dp, int n)
  258. {
  259.     while (n-- > 0) {
  260.     if (dp->tdir_type == TIFF_SHORT || dp->tdir_type == TIFF_SSHORT)
  261.         dp->tdir_offset <<= 16;
  262.     else if (dp->tdir_type == TIFF_BYTE || dp->tdir_type == TIFF_SBYTE)
  263.         dp->tdir_offset <<= 24;
  264.     dp++;
  265.     }
  266. }
  267.  
  268. private int
  269. faxout_begin_page(TIFFO